#! /usr/bin/env python
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
import sys, os, sqlite3, todo
from elixir import *
from ventanaPrincipalUI import Ui_MainWindow
from articulo import FormArticulo
from vendedor import FormVendedor
from nuevaVenta import FormNuevaVenta
from devolucion import FormDevolucion
from anulacion import FormAnulacion

class LoginDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(LoginDialog, self).__init__(parent)

        self.username = QtGui.QLineEdit()
        self.password = QtGui.QLineEdit()
        self.password.setEchoMode (QtGui.QLineEdit.Password)
        loginLayout = QtGui.QFormLayout()
        self.setWindowTitle("Login - Indigo")
        loginLayout.addRow("Usuario", self.username)
        loginLayout.addRow("Clave", self.password)

        self.buttons = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel)
        self.buttons.accepted.connect(self.check)
        self.buttons.rejected.connect(self.reject)

        layout = QtGui.QVBoxLayout()
        layout.addLayout(loginLayout)
        layout.addWidget(self.buttons)
        self.setLayout(layout)

    def check(self):
        usuario = todo.Vendedor.get_by(usuario=unicode(self.username.text()))
        if str(self.password.text()) == usuario.password: # do actual login check
            self.accept()
        else:
            QtGui.QMessageBox.warning(self, 'Mensaje',
                "El usuario y/o clave no corresponden.")

class Myform(QtGui.QMainWindow):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.ui = Ui_MainWindow()
        self.center()
        self.ui.setupUi(self)

    def center(self):
        qr = self.frameGeometry()
        cp = QtGui.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def articulos(self):
        self.ventanaArticulo = FormArticulo()
        self.ventanaArticulo.show()

    def vendedores(self):
        self.ventanaVendedor = FormVendedor()
        self.ventanaVendedor.show()

    def anulacion(self):
        self.ventanaAnulacion = FormAnulacion()
        self.ventanaAnulacion.show()

    def nuevaVenta(self):
        self.ventanaNuevaVenta = FormNuevaVenta()
        self.ventanaNuevaVenta.setVendedor(self.vendedor)
        self.ventanaNuevaVenta.show()
        
    def devolucion(self):
        self.ventanaDevolucion = FormDevolucion()
        self.ventanaDevolucion.setVendedor(self.vendedor)
        self.ventanaDevolucion.show()

    def setUsername(self, username):
        self.vendedor = todo.Vendedor.get_by(usuario=unicode(username))
        self.ui.labelUsuario.setText("%s %s" % (self.vendedor.nombre, self.vendedor.apellido_paterno))

if __name__ == '__main__':
    todo.initDB()
    app = QtGui.QApplication(sys.argv)

    login = LoginDialog()
    if not login.exec_(): # 'reject': user pressed 'Cancel', so quit
        sys.exit(-1)

    myapp = Myform()
    myapp.setUsername(login.username.text())
    myapp.show()
    sys.exit(app.exec_())